/*
============================================================================
 Name        : pd_font.cpp
 Author      : Jian(raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : pd_font.cpp - source file
============================================================================
*/

/* INCLUDE FILES */
#include "pd_font.h"
#include "PdDefine.h"

#include <eikenv.h>
#include <gdi.h>


pd_font* pd_font_create(pd_font_size size, pd_font_type type) 
{
    CFont* ret = NULL ;

    TFontSpec sFontSpec = CEikonEnv::Static()->DenseFont()->FontSpecInTwips() ;
    TFontStyle aFontStyle ;

    TInt height = sFontSpec.iHeight ;
    if (size == PD_SMALL_FONT) 
        height = sFontSpec.iHeight * 0.8 ;
    else if (size == PD_LARGE_FONT)
        height = sFontSpec.iHeight * 1.2 ;
    
    sFontSpec.iHeight = CEikonEnv::Static()->ScreenDevice()->VerticalPixelsToTwips(size) ;

    if (type == PD_FONT_BOLD)
    {
        aFontStyle.SetPosture(EPostureUpright);
        aFontStyle.SetStrokeWeight(EStrokeWeightBold);
        sFontSpec.iFontStyle = aFontStyle;
    }
    else if (type == PD_FONT_ITALIC) 
    {
        aFontStyle.SetPosture(EPostureItalic);
        aFontStyle.SetStrokeWeight(EStrokeWeightNormal);
        sFontSpec.iFontStyle = aFontStyle;
    }
    else 
    {
        aFontStyle.SetPosture(EPostureUpright);
        aFontStyle.SetStrokeWeight(EStrokeWeightNormal);
        sFontSpec.iFontStyle = aFontStyle;
    }

    CEikonEnv::Static()->ScreenDevice()->GetNearestFontInTwips(ret, sFontSpec);

    if (!ret)
        DD_LOG("pd_font_create create the font size = %d and type = %d is error", size, type) ;

    return (pd_font*)ret ;
}

void pd_font_destroy(pd_font* font) 
{
    __DD_ASSERT_DEFENSE(font, return) ;

    CEikonEnv::Static()->ScreenDevice()->ReleaseFont((CFont*)font) ;
}

DD_UINT pd_font_max_height(pd_font* font) 
{
    __DD_ASSERT_DEFENSE(font, return 0) ;

    CFont* sFont = (CFont*)font ;
    return sFont->FontMaxHeight() ;
}

DD_UINT pd_font_text_width(pd_font* font, const DD_UNICODE* text) 
{
    __DD_ASSERT_DEFENSE(font, return 0) ;
    __DD_CHECK_PTR(text, DD_UNICODE, return 0) ;
    
    CFont* sFont = (CFont*)font ;

    return sFont->TextWidthInPixels(__ToPtrC16(text)) ;
}

DD_UINT pd_font_char_width(pd_font* font, DD_UNICODE chr) 
{
    __DD_ASSERT_DEFENSE(font, return 0) ;

    CFont* sFont = (CFont*)font ;
    return sFont->CharWidthInPixels(chr) ;
}

DD_UINT pd_font_count_char(pd_font* font, DD_INT width, const DD_UNICODE* text) 
{
    __DD_ASSERT_DEFENSE(font && width >= 0, return 0) ;
    __DD_CHECK_PTR(text, DD_UNICODE, return 0) ;

    CFont* sFont = (CFont*)font ;
    TPtrC pText(__ToPtrC16(text)) ;
    TInt excessWidth = 0 ;
    return sFont->TextCount(pText, width, excessWidth) ;
}

/* End of File */
